package com.lonely.fx.property;

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * @date: 2022-07-21 17:58
 * @author: ztkj-hzb
 * @description: 针对 Object类型的 监听包装
 **/
public class ObjectPropertySettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane gridPane = new GridPane();
        gridPane.setVgap(20);
        gridPane.setHgap(20);

        Label label = new Label("请输入待修改的值(name)");
        TextField textField = new TextField();
        Button button = new Button("确定");

        Label label2 = new Label("请输入待修改的值(age)");
        TextField textField2 = new TextField();
        Button button2 = new Button("确定");


        TextArea textArea = new TextArea();
        textArea.setStyle("-fx-background-color: pink");

        gridPane.add(label, 0, 0);
        gridPane.add(textField, 1, 0);
        gridPane.add(button, 2, 0);
        gridPane.add(label2, 0, 1);
        gridPane.add(textField2, 1, 1);
        gridPane.add(button2, 2, 1);
        gridPane.add(textArea, 0, 2, 3, 1);


        User user = new User();
        user.nameProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("监听到name值修改了");
            textArea.appendText(newValue);
        });
        user.ageProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("监听到age值修改了");
            textArea.appendText(newValue + "");
        });

        button.setOnAction(event -> {
            String text = textField.getText();
            user.setName(text);
        });

        button2.setOnAction(event -> {
            String text = textField2.getText();
            user.setAge(Integer.parseInt(text));
        });


        primaryStage.setScene(new Scene(gridPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }


    /**
     * 模拟javafx提供的属性类
     */
    private class User {

        private String name;

        private Integer age;

        private SimpleStringProperty nameProperty;

        private SimpleIntegerProperty ageProperty;


        /**
         * 获取nameProperty
         *
         * @return
         */
        public SimpleStringProperty nameProperty() {
            if (nameProperty == null) {
                nameProperty = new SimpleStringProperty(this, "name", this.name);
            }
            return nameProperty;
        }

        /**
         * 获取ageProperty
         *
         * @return
         */
        public SimpleIntegerProperty ageProperty() {
            if (ageProperty == null) {
                ageProperty = new SimpleIntegerProperty(this, "age", this.age == null ? 0 : this.age);
            }
            return ageProperty;
        }


        public String getName() {
            if (nameProperty != null) {
                return this.nameProperty.get();
            } else {
                return this.name;
            }
        }

        public void setName(String name) {

            if (nameProperty != null) {
                this.nameProperty.set(name);
            } else {
                this.name = name;
            }
        }

        public Integer getAge() {
            if(ageProperty != null){
                return ageProperty.get();
            }else {
                return this.age;
            }
        }

        public void setAge(Integer age) {
            if(ageProperty != null){
                this.ageProperty.set(age);
            }else {
                this.age = age;
            }
        }
    }

}
